package com.cainaer.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cainaer.common.core.utils.StringUtils;
import com.cainaer.product.domain.*;
import com.cainaer.product.mapper.ProductMapper;
import com.cainaer.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author Tina
 * @since 2020-09-11
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    /**
     * 新增商品
     *
     * @param product
     * @return
     */
    @Override
    public Integer insertProduct(Product product) {

        return productMapper.insert(product);
    }

    /**
     * 条件分页查询商品
     *
     * @param product
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public IPage<Product> selectProduct(Product product, Integer pageNum, Integer pageSize) {
        Page<Product> iPage = new Page<Product>(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper<Product>();
        if (StringUtils.isNotNull(product.getTypeId())) {
            queryWrapper.eq("type_id", product.getTypeId());
        }
        return productMapper.selectPage(iPage, queryWrapper);
    }

    /**
     * 编辑商品
     *
     * @param product
     * @return
     */
    @Override
    public Integer editProduct(Product product) {
        return productMapper.updateById(product);
    }

    /**
     * 查询商品
     *
     * @param product
     * @return
     */
    @Override
    public List<Product> selectProduct(Product product) {
        QueryWrapper queryWrapper = new QueryWrapper<Product>();

        if (StringUtils.isNotNull(product.getProductId())) {
            queryWrapper.eq("product_id", product.getProductId());
        }
        if (StringUtils.isNotNull(product.getShopId())) {
            queryWrapper.eq("shop_id", product.getShopId());
        }
        if (StringUtils.isNotNull(product.getCategoryId())) {
            queryWrapper.eq("category_id", product.getCategoryId());
        }
        if (StringUtils.isNotNull(product.getProductName())) {
            queryWrapper.eq("product_name", product.getProductName());
        }
        if (StringUtils.isNotNull(product.getStatus())) {
            queryWrapper.eq("status", product.getStatus());
        } else {
            queryWrapper.ne("status", 4);
            queryWrapper.ne("status", 1);
        }
        queryWrapper.orderByDesc("updated_time");
        return productMapper.selectList(queryWrapper);
    }

    /**
     * 查询商品详情信息
     *
     * @param productId
     * @return
     */
    @Override
    public List<Product> selectProductInfo(Long productId) {
        return productMapper.selectProductInfo(productId);
    }


}
